package com.markerhub.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.InspectionRecord;
import com.markerhub.service.InspectionRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 巡检记录Controller
 */
@Api(tags = "巡检记录管理")
@RestController
@RequestMapping("/api/inspection-record")
public class InspectionRecordController extends BaseController {

    @Autowired
    private InspectionRecordService inspectionRecordService;

    /**
     * 获取巡检记录列表 - 分页
     */
    @ApiOperation(value = "获取巡检记录列表", notes = "获取所有巡检记录信息，支持分页查询")
    @PreAuthorize("hasAuthority('inspection:record:list')")
    @GetMapping("/list")
    public Result list(InspectionRecord record) {
        Page<InspectionRecord> page = inspectionRecordService.page(getPage(), new QueryWrapper<InspectionRecord>()
                .like(record.getDeviceId() != null, "device_id", record.getDeviceId())
                .eq(record.getStatus() != null, "status", record.getStatus())
                .eq(record.getTaskId() != null, "task_id", record.getTaskId()));
        return Result.succ(200, "获取巡检记录列表成功", page);
    }

    /**
     * 查询巡检记录 - 分页
     */
    @ApiOperation(value = "查询巡检记录", notes = "根据条件查询巡检记录信息，支持分页")
    @PreAuthorize("hasAuthority('inspection:record:query')")
    @GetMapping("/query")
    public Result query(InspectionRecord record) {
        return list(record);
    }

    /**
     * 根据ID获取巡检记录信息
     */
    @ApiOperation(value = "获取巡检记录详情", notes = "根据记录ID获取指定巡检记录的详细信息")
    @PreAuthorize("hasAuthority('inspection:record:list')")
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Long id) {
        InspectionRecord record = inspectionRecordService.getById(id);
        return Result.succ(200, "获取巡检记录信息成功", record);
    }

    /**
     * 创建巡检记录
     */
    @ApiOperation(value = "新增巡检记录", notes = "新增巡检记录信息")
    @PreAuthorize("hasAuthority('inspection:record:add')")
    @PostMapping("/add")
    public Result add(@Validated @RequestBody InspectionRecord record) {
        boolean saved = inspectionRecordService.save(record);
        return saved ? Result.succ(200, "创建巡检记录成功", record) : Result.fail("创建巡检记录失败");
    }

    /**
     * 更新巡检记录
     */
    @ApiOperation(value = "更新巡检记录", notes = "更新指定巡检记录的详细信息")
    @PreAuthorize("hasAuthority('inspection:record:update')")
    @PutMapping("/update")
    public Result update(@Validated @RequestBody InspectionRecord record) {
        boolean updated = inspectionRecordService.updateById(record);
        return updated ? Result.succ(200, "更新巡检记录成功", record) : Result.fail("更新巡检记录失败");
    }

    /**
     * 删除单个巡检记录
     */
    @ApiOperation(value = "删除巡检记录", notes = "删除指定ID的巡检记录信息")
    @PreAuthorize("hasAuthority('inspection:record:delete')")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean deleted = inspectionRecordService.removeById(id);
        return deleted ? Result.succ(200, "删除巡检记录成功", null) : Result.fail("删除巡检记录失败");
    }

    /**
     * 批量删除巡检记录
     */
    @ApiOperation(value = "批量删除巡检记录", notes = "批量删除多个巡检记录信息")
    @PreAuthorize("hasAuthority('inspection:record:delete')")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody Long[] ids) {
        boolean deleted = inspectionRecordService.removeByIds(Arrays.asList(ids));
        return deleted ? Result.succ(200, "批量删除巡检记录成功", null) : Result.fail("批量删除巡检记录失败");
    }
}